﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ElementsVisibleInLayoutFilter.h"
#include "IElement.h"
#include "DbObjectUtils.h"
#include "IGenericElement.h"
#include "IAnnotationText.h"
#include "IDrawingViewport.h"
#include "IDrawingLine.h"
#include "IDrawingLayout.h"
#include "IPureGraphicsElement.h"
#include "IElementShapeHandle.h"
#include "IDrawingAnnotationText.h"
#include "IRevisionCloud.h"
#include "IModelLine.h"
#include "IModelView.h"
#include "IDrawingTable.h"
#include "IDrawingTableItem.h"
#include "IDrawingPolyCurve.h"
#include "NdbUpgrade.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

CREATE_DEBUG_MODE(ShowModelLineInLayout, L"在图纸上是否可以显示模型线", gcmp::DebugModeGroup::DMGT_OTHERS, L"GDMPLab", L"2023-12-20", L"启用此模式后，在图纸上可以显示模型线。");

using namespace gcmp;


DBOBJECT_DATA_DEFINE(ElementsVisibleInLayoutFilter)
{
    SetIsFilterEnabled__(true);
}

const std::wstring& ElementsVisibleInLayoutFilter::GetUniqueName() const
{
    static const std::wstring filterName = L"ElementsVisibleInLayoutFilter";
    return filterName;
}

bool ElementsVisibleInLayoutFilter::IsElementVisible(const IModelView* pModelView, const IElement* pElement) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");

    const IGenericElement* pGenericElement = quick_cast<const IGenericElement>(pElement);
    if (pGenericElement)
    {
        const IExternalObject* pExternalObject = pGenericElement->GetExternalObject();
        if (!pExternalObject)
        {
            return false;
        }
        if (quick_cast<const IDrawingTable> (pExternalObject)
            || quick_cast<const IDrawingTableItem> (pExternalObject) 
            || quick_cast<const IDrawingViewport>(pExternalObject) 
            || quick_cast<const IDrawingLayout>(pExternalObject) 
            || quick_cast<const IDrawingAnnotationText>(pExternalObject) 
            || (quick_cast<const IModelLine> (pExternalObject) && DEBUG_MODE(ShowModelLineInLayout))
            || quick_cast<const IRevisionCloud>(pExternalObject)
            || quick_cast<const IDrawingPolyCurve>(pExternalObject)
            )
        {
            return true;
        }
    }

    if (IsA<gcmp::IAnnotationText>(pElement) || IsA<gcmp::IDrawingLine>(pElement) || IsA<gcmp::IPureGraphicsElement>(pElement) || IsA<gcmp::IElementShapeHandle>(pElement))
    {
        return true;
    }
    return false;
}

bool gcmp::ElementsVisibleInLayoutFilter::IsElementEditable(const IModelView * pModelView, const IElement * pElement) const
{
    return true;
}

void ElementsVisibleInLayoutFilter::EnableFilter(bool enable)
{
    SetIsFilterEnabled__(enable); 
}

bool ElementsVisibleInLayoutFilter::IsFilterEnabled() const
{ 
    return GetIsFilterEnabled__();
}
